package com.baizhi.springbootday0202.service.impl;

import com.baizhi.springbootday0202.dao.AuctionDao;
import com.baizhi.springbootday0202.entity.Auction;
import com.baizhi.springbootday0202.service.AuctionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
public class AuctionServiceImpl implements AuctionService {

	@Autowired(required = false)
	private AuctionDao auctionDao;
	@Override
	@Transactional(readOnly = true)
	public Map selectAuction(int page, int limit) {

//		参数处理
		int offset = (page-1)*limit;

//		查询数据并将结果以指定的格式封装在map中
		List<Auction> auctions = auctionDao.selectAuction(offset, limit);
		int count = auctionDao.totalCount();
		Map<String,Object> map = new HashMap<>();
		map.put("data",auctions);
		map.put("count",count);
		map.put("code",0);
		map.put("msg","");

//		返回结果
		return map;
	}

	@Override
	public Map updateAuction(Auction auction) {
		Map<String,Object> map = new HashMap<>();
		int i = auctionDao.updateAuction(auction);
		if(i > 0){
			map.put("isUpdate","修改成功");
		}else{
			map.put("isUpdate","修改失败");
		}

		return map;
	}

	@Override
	@RequestMapping("/deleteAuction")
	public Map deleteAuction(int id) {
		Map<String,Object> map  = new HashMap<>();
		int i = auctionDao.deleteAuction(id);
		if(i > 0){
			map.put("isDelete","删除成功");
		}else{
			map.put("isDelete","删除失败");
		}
		return map;
	}

	@Override
	@RequestMapping("/insertAuction")
	@ResponseBody
	public Map insertAuction(Auction auction) {
		Map<String,Object> map = new HashMap<>();
		int i = auctionDao.insertAuction(auction);
		if (i > 0) {
			map.put("isInsert","发布成功");
		}else{
			map.put("isInsert","发布失败");
		}
		return map;
	}

	@Override
	public Auction selectById(int id) {
		return auctionDao.selectById(id);
	}
}
